<template>
  <div class="app-container" style="background-color:#f3f5fa;">

    <!-- 添加或修改商户信息对话框 -->
    <div style="display:inline-block;background-color:#ffffff;width:100%;max-width:800px;min-width:500px;border-radius:5px;padding:30px;vertical-align:top;margin:0 auto;display: block;">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <div>
        <el-form-item label="活动主题" prop="subject">
          <el-input v-model="form.subject" placeholder="请输入活动主题" />
        </el-form-item>
        <el-form-item label="活动内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="活动类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择活动类型">
            <el-option
            v-for="dict in activityTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="活动开始时间" prop="startDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.startDate"
            type="date"
            value-format="yyyyMMdd"
            placeholder="选择活动开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="endDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.endDate"
            type="date"
            value-format="yyyyMMdd"
            placeholder="选择活动结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动主图" prop="showMainImg">
          <uploadImage v-model="form.showMainImg"/>
        </el-form-item>
        <el-form-item label="活动图片1">
          <uploadImage v-model="form.showImg1"/>
        </el-form-item>
        <el-form-item label="活动图片2">
          <uploadImage v-model="form.showImg2"/>
        </el-form-item>
        <el-form-item label="活动图片3">
          <uploadImage v-model="form.showImg3"/>
        </el-form-item>
        <el-form-item label="活动外链" prop="linkUrl">
          <el-input v-model="form.linkUrl" placeholder="请输入活动外链" />
        </el-form-item>
        <el-form-item label="银行/商户" prop="bankName">
          <customSelect
            v-model="form.bankName"
            :dataList="bankOptions"
            labelKey="cnName"
            valueKey="no"
            v-on:selectPageChange='getBankOptions'
            v-on:selectChange="updateBank"
            :pageInfo="queryParams"
            @click="getBankOptions"
          />
        </el-form-item>
        <el-form-item label="用户奖励产品" prop="customerRewardProductName">
          <customSelect
            v-model="form.customerRewardProductName"
            :dataList="customerRewardProductOptions"
            labelKey="name"
            valueKey="no"
            v-on:selectPageChange='getCustomerRewardProductOptions'
            v-on:selectChange="updateCustomerRewardProduct"
            :pageInfo="queryParams"
            @click="getCustomerRewardProductOptions"
          />
        </el-form-item>
        <el-form-item label="商户奖励类型" prop="merchantRewardType">
          <el-select v-model="form.merchantRewardType" placeholder="请选择商户奖励类型">
            <el-option
            v-for="dict in activityRewardTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="商户奖励数值" prop="merchantRewardNumber">
          <el-input v-model="form.merchantRewardNumber" placeholder="请输入商户奖励数值(如果是比例就填0-100自然数)" />
        </el-form-item>
        <el-form-item label="活动推送消息模板" prop="messageTemplateNo">
          <customSelect
            v-model="form.messageTemplateName"
            :dataList="messageTemplateOptions"
            labelKey="name"
            valueKey="no"
            v-on:selectPageChange='getMessageTemplateOptions'
            v-on:selectChange="updateMessageTemplateOptions"
            :pageInfo="queryParams"
            @click="getMessageTemplateOptions"
          />
        </el-form-item>
        <el-form-item label="推送时间" prop="activityPushInfo.pushTimeType" :rules="{ required: true, message: '推送时间不能为空', trigger: 'blur' }">
          <el-select v-model="form.activityPushInfo.pushTimeType" placeholder="请选择推送时间">
            <el-option
            v-for="dict in activityPushDateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.activityPushInfo.pushTimeType == 2" label="推送开始日期" prop="activityPushInfo.pushStartDate" :rules="{ validator: pushStartDateRule, trigger: 'blur' }">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.activityPushInfo.pushStartDate"
            type="date"
            value-format="yyyyMMdd"
            placeholder="推送开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="form.activityPushInfo.pushTimeType == 2" label="推送时间" prop="activityPushInfo.pushTime" :rules="{ validator: pushTimeRule, trigger: 'blur' }">
          <el-time-picker
            v-model="form.activityPushInfo.pushTime"
            value-format="HHmmss"
            placeholder="推送时间">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="推广人群" prop="activityPushInfo.pushCrowdType" :rules="{ required: true, message: '推广人群不能为空', trigger: 'blur' }">
          <el-select v-model="form.activityPushInfo.pushCrowdType" placeholder="请选择推广人群">
            <el-option
            v-for="dict in activityPushScopeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.activityPushInfo.pushCrowdType != 1" label="定向推广类型" prop="activityPushInfo.directPushType" :rules="{ validator: directPushTypeRule, trigger: 'blur' }">
          <el-select v-model="form.activityPushInfo.directPushType" placeholder="请选择定向推广类型">
            <el-option
            v-for="dict in activityPushDirectionTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.activityPushInfo.pushCrowdType != 1 && form.activityPushInfo.directPushType == 1" label="起始注册时间" prop="activityPushInfo.startRegisterDate" :rules="{ validator: startRegisterDateRule, trigger: 'blur' }">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.activityPushInfo.startRegisterDate"
            type="date"
            value-format="yyyyMMdd"
            placeholder="起始注册时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="form.activityPushInfo.pushCrowdType != 1 && form.activityPushInfo.directPushType == 1" label="结束注册时间" prop="activityPushInfo.endRegisterDate" :rules="{ validator: endRegisterDateRule, trigger: 'blur' }">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.activityPushInfo.endRegisterDate"
            type="date"
            value-format="yyyyMMdd"
            placeholder="结束注册时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="form.activityPushInfo.pushCrowdType != 1 && form.activityPushInfo.directPushType == 2" label="用户编号规则" prop="activityPushInfo.customerNoPattern" :rules="{ validator: customerNoPatternRule, trigger: 'blur' }">
          <el-input v-model="form.activityPushInfo.customerNoPattern" placeholder="请输入用户编号规则" />
        </el-form-item>
        <el-form-item v-if="form.activityPushInfo.pushCrowdType != 1 && form.activityPushInfo.directPushType == 3" label="用户状态" prop="activityPushInfo.customerStatus" :rules="{ validator: customerStatusRule, trigger: 'blur' }">
          <el-select v-model="form.activityPushInfo.customerStatus" placeholder="请选择用户状态">
            <el-option
            v-for="dict in customerStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <div slot="footer" class="dialog-footer" style="text-align:center;">
          <el-button type="primary" @click="submitForm">保 存</el-button>
          <el-button v-hasPermi="['activity:info:list']">
            <router-link :to="{path:'/activity/info'}">列表页面</router-link>
          </el-button>
        </div>
      </div>
      </el-form>

    </div>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo, getInfoVo, addInfoVo, updateInfoVo, logicDelInfo, getInfoVoByNo } from "@/api/activity/info";
import { listBasic, getBasic, getBasicByNo, delBasic, addBasic, updateBasic, exportBasic, getMerchantAuditMessage, auditBasic, listBasicOptions } from "@/api/merchant/basic";
import { listInfoOptions as listProduct } from "@/api/product/info";
import { listTemplateOptions } from "@/api/message/template";
import UploadImage from '@/components/UploadImage';
import CustomSelect from '@/components/Select';

export default {
  name: "Info",
  components: {
    UploadImage,
    CustomSelect,
  },
  data() {
    return {
      // 商户编号
      merchantNo: this.$route.params.merchantNo,
      // 商户编号
      merchantName: this.$route.params.merchantName,
      // 活动编号
      no: this.$route.params.no,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动信息表格数据
      infoList: [],
      // 银行集合
      bankOptions: [],
      // 用户奖励产品集合
      customerRewardProductOptions: [],
      // 消息模板集合
      messageTemplateOptions: [],
      // 活动类型字典
      activityTypeOptions: [],
      // 活动奖励类型字典
      activityRewardTypeOptions: [],
      // 活动推送时间字典
      activityPushDateOptions: [],
      // 活动推送范围字典
      activityPushScopeOptions: [],
      // 活动定向推送类型字典
      activityPushDirectionTypeOptions: [],
      // 用户状态字典
      customerStatusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        cnName: null,
        type: null
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        subject: [
          { required: true, message: "活动主题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "活动内容不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "活动类型不能为空", trigger: "blur" }
        ],
        startDate: [
          { required: true, message: "活动开始时间不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "活动结束时间不能为空", trigger: "blur" }
        ],
        showMainImg: [
          { required: true, message: "活动主图不能为空", trigger: "blur" }
        ],
        linkUrl: [
          { required: true, message: "活动外链不能为空", trigger: "blur" }
        ],
        bankName: [
          { required: true, message: "银行/商户不能为空", trigger: "blur" }
        ],
        customerRewardProductName: [
          { required: true, message: "用户奖励产品不能为空", trigger: "blur" }
        ],
        merchantRewardType: [
          { required: true, message: "商户奖励类型不能为空", trigger: "blur" }
        ],
        merchantRewardNumber: [
          { required: true, message: "商户奖励数值不能为空", trigger: "blur" }
        ],
        pushTimeType: [
          { required: true, message: "推送时间不能为空", trigger: "blur" }
        ],
        messageTemplateNo: [
          { required: true, message: "消息模板不能为空", trigger: "blur" }
        ]
      }
    };
  },
  //每次进入页面时触发
  activated(){
    this.reset();
    this.no = this.$route.params.no;
    this.merchantNo = this.$route.params.merchantNo;
    this.merchantName = this.$route.params.merchantName;
    if(this.merchantNo){
      this.form.bankNo = this.merchantNo;
      this.form.bankName = this.merchantName;
      this.getBankOptions(null,null,this.merchantName);
    }
    if(this.no){
      getInfoVoByNo(this.no).then(response => {
        this.form = response.data;
        this.getCustomerRewardProductOptions(null,null,this.form.customerRewardProductName);
        this.getBankOptions(null,null,this.form.belongMerchantName);
        this.getMessageTemplateOptions(null,null,null,this.form.messageTemplateNo);
      });
    }
  },
  created() {
    this.reset();
    this.getDicts("act_activity_type").then(response => {
      this.activityTypeOptions = response.data;
    });
    this.getDicts("act_activity_reward_type").then(response => {
      this.activityRewardTypeOptions = response.data;
    });
    this.getDicts("act_activity_push_date").then(response => {
      this.activityPushDateOptions = response.data;
    });
    this.getDicts("act_activity_push_scope").then(response => {
      this.activityPushScopeOptions = response.data;
    });
    this.getDicts("act_activity_push_direction_type").then(response => {
      this.activityPushDirectionTypeOptions = response.data;
    });
    this.getDicts("ctm_customer_status").then(response => {
      this.customerStatusOptions = response.data;
    });
    this.getDicts("ctm_customer_status").then(response => {
      this.customerStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询活动信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        subject: null,
        content: null,
        type: null,
        startDate: null,
        endDate: null,
        showMainImg: null,
        showImg1: null,
        showImg2: null,
        showImg3: null,
        linkUrl: null,
        bankNo: null,
        bankName: null,
        status: "0",
        customerRewardProductNo: null,
        customerRewardProductName: null,
        merchantRewardType: null,
        merchantRewardNumber: null,
        belongMerchantNo: null,
        belongMerchantName: null,
        examineCode: null,
        activityPushNo: null,
        buildOperator: null,
        buildDate: null,
        buildTime: null,
        modifiedOperator: null,
        modifiedDate: null,
        modifiedTime: null,
        messageTemplateNo: null,
        messageTemplateName: null,
        activityPushInfo: {
          pushNo: null,
          pushTimeType: null,
          pushTime: null,
          pushStartDate: null,
          pushEndDate: null,
          pushCrowdType: "1",
          directPushType: null,
          startRegisterDate: null,
          endRegisterDate: null,
          customerNoPattern: null,
          customerStatus: null,
          buildOperator: null,
          buildDate: null,
          buildTime: null,
          modifiedOperator: null,
          modifiedDate: null,
          modifiedTime: null
        }
        
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.nos = selection.map(item => item.no)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfoVo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          var actPush = this.form.activityPushInfo;
          if(actPush.pushTimeType == 1){
            actPush.pushStartDate = null;
            actPush.pushTime = null;
          }
          if(actPush.pushCrowdType == 1){
            actPush.startRegisterDate = null;
            actPush.endRegisterDate = null;
            actPush.customerNoPattern = null;
            actPush.customerStatus = null;
            actPush.directPushType = null;
          }
          if(actPush.directPushType == 1){
            actPush.customerNoPattern = null;
            actPush.customerStatus = null;
          }else if(actPush.directPushType == 2){
            actPush.startRegisterDate = null;
            actPush.endRegisterDate = null;
            actPush.customerStatus = null;
          }else if(actPush.directPushType == 3){
            actPush.startRegisterDate = null;
            actPush.endRegisterDate = null;
            actPush.customerNoPattern = null;
          }
          if (this.form.id != null) {
            updateInfoVo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfoVo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const nos = row.no || this.nos;
      this.$confirm('是否确认删除活动信息编号为"' + nos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicDelInfo(nos);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有活动信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInfo(queryParams);
        }).catch(() => {});
    },
    /** 推送开始日期表单验证 */
    pushStartDateRule(rule, value, callback) {
      if(this.form.activityPushInfo.pushTimeType == 2&&!value){
        callback("推送开始日期不能为空");
        return;
      }
      callback();
    },
    /** 推送时间表单验证 */
    pushTimeRule(rule, value, callback) {
      if(this.form.activityPushInfo.pushTimeType == 2&&!value){
        callback("推送时间不能为空");
        return;
      }
      callback();
    },
    /** 定向推广类型表单验证 */
    directPushTypeRule(rule, value, callback) {
      if(this.form.activityPushInfo.pushCrowdType != 1&&!value){
        callback("定向推广类型不能为空");
        return;
      }
      callback();
    },
    /** 起始注册时间表单验证 */
    startRegisterDateRule(rule, value, callback) {
      if(this.form.activityPushInfo.pushCrowdType != 1&&this.form.activityPushInfo.directPushType == 1&&!value){
        callback("起始注册时间不能为空");
        return;
      }
      callback();
    },
    /** 结束注册时间表单验证 */
    endRegisterDateRule(rule, value, callback) {
      if(this.form.activityPushInfo.pushCrowdType != 1&&this.form.activityPushInfo.directPushType == 1&&!value){
        callback("结束注册时间不能为空");
        return;
      }
      callback();
    },
    /** 用户编号规则表单验证 */
    customerNoPatternRule(rule, value, callback) {
      if(this.form.activityPushInfo.pushCrowdType != 1&&this.form.activityPushInfo.directPushType == 2&&!value){
        callback("用户编号规则不能为空");
        return;
      }
      callback();
    },
    /** 用户状态表单验证 */
    customerStatusRule(rule, value, callback) {
      if(this.form.activityPushInfo.pushCrowdType != 1&&this.form.activityPushInfo.directPushType == 3&&!value){
        callback("用户状态不能为空");
        return;
      }
      callback();
    },
    getBankOptions(pageNum,pageSize,queryContent){
      if(!pageNum){
        pageNum = this.queryParams.pageSize;
      }
      if(!pageSize){
        pageSize = this.queryParams.pageSize;
      }
      this.queryParams.pageNum = pageNum;
      this.queryParams.pageSize = pageSize;
      this.queryParams.cnName = queryContent;

      this.loading = true;
      listBasicOptions(this.queryParams).then(response => {
        this.bankOptions = response.rows;
        this.queryParams.total = response.total;
        this.loading = false;
      });
    },
    updateBank(val){
      var valArr = val.split("-");
      this.form.bankNo = valArr[0];
      this.form.bankName = valArr[1];
      this.form.belongMerchantNo = this.form.bankNo;
      this.form.belongMerchantName = this.form.bankName;
    },
    updateCustomerRewardProduct(val){
      var valArr = val.split("-");
      this.form.customerRewardProductNo = valArr[0];
      this.form.customerRewardProductName = valArr[1];
    },
    getCustomerRewardProductOptions(pageNum,pageSize,queryContent){
      if(!pageNum){
        pageNum = this.queryParams.pageSize;
      }
      if(!pageSize){
        pageSize = this.queryParams.pageSize;
      }
      this.queryParams.pageNum = pageNum;
      this.queryParams.pageSize = pageSize;
      this.queryParams.name = queryContent;

      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.customerRewardProductOptions = response.rows;
        this.queryParams.total = response.total;
        this.loading = false;
      });
    },
    updateMessageTemplateOptions(val){
      var valArr = val.split("-");
      this.form.messageTemplateNo = valArr[0];
      this.form.messageTemplateName = valArr[1];
    },
    getMessageTemplateOptions(pageNum,pageSize,queryContent,no){
      if(!pageNum){
        pageNum = this.queryParams.pageSize;
      }
      if(!pageSize){
        pageSize = this.queryParams.pageSize;
      }
      this.queryParams.pageNum = pageNum;
      this.queryParams.pageSize = pageSize;
      this.queryParams.name = queryContent;
      this.queryParams.no = no;
      this.queryParams.type = 2;

      this.loading = true;
      listTemplateOptions(this.queryParams).then(response => {
        this.messageTemplateOptions = response.rows;
        if(no){
          this.messageTemplateOptions.forEach(element => {
            if(element.no == no){
              this.form.messageTemplateNo = element.no
              this.form.messageTemplateName = element.name
            }
          });
        }
        this.queryParams.total = response.total;
        this.loading = false;
      });
    }
  }
};
</script>
